<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
    <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0"/>
    <title>title</title>
    <link rel="stylesheet" type="text/css" href="../css/api.css"/>
    <link rel="stylesheet" type="text/css" href="../css/aui.css" />
     <link rel="stylesheet" type="text/css" href="../css/aui.2.0.css" />
    <link rel="stylesheet" type="text/css" href="../css/aui-flex.css" />
    <link rel="stylesheet" type="text/css" href="../css/aui-list-swipe.css" />
    <style>
    	body{

    	}
    </style>
</head>
<body>
    <header id='header' class="aui-bar aui-bar-nav" style="background-color: #03a9f4">
        <a class="aui-pull-left aui-btn" onclick="api.closeWin();">
            <span class="aui-iconfont aui-icon-left"></span>
        </a>
        <div class="aui-title" id="middle">
            配送备货
        </div>
    </header>
	<div class="aui-content">

	    <div class="aui-form">

	        <div class="aui-input-row" style="font-size: 18px;">
	            <span class="aui-input-addon" style="width: 20%;text-align: left;font-size: 0.8rem;font-weight: 600;">配送任务号:</span>
	            <input type="text" class='aui-input' placeholder="" style="width: 80%;" id="paTaskCode" />
	        	<i class="aui-input-addon aui-iconfont aui-icon-scan " style="font-size: 1.6rem" onclick="doScanDaqInfo()" ></i>
	        </div>

	    </div>
        <div style="background-color:white; height: 100%;width: 100%;font-size: 16px;">
	    	<table style="width:100%;margin-left: 5px;">
                <tr>
                    <td   style="width: 25%;">配送单号：</td>
                    <td id="code" colspan="3" ></td>

                </tr>
                <tr>
                    <td   style="width: 25%;">工单号：</td>
                    <td id="workOrderCode" colspan="3"  ></td>
                </tr>
                <tr>
                    <td style="width: 25%;">目标库：</td>
                    <td id="uda1c" style="width: 25%;"></td>
                    <td style="width: 25%;">源库：</td>
                    <td id="uda2c" style="width: 25%;"></td>
                </tr>
            </table>

	    </div>
		<div style="background-color:white; height: 100%;width: 100%;">
            <table class="table" style='width:100%;' id="table">
                <thead>
                    <tr style="text-align: center;background-color: #DCE9F2;color: #467aa7;font-size: 12px;font-weight: bold;">
                        <!-- <td algin='center' style="width:50px;">序号</td> -->
                        <td algin='center' style="width:120px;;">物料号</td>
                        <td algin='center' style="width:180px;">物料名</td>
                        <td algin='center' style="width:100px;">理论数量</td>
                        <td algin='center' style="width:100px;">计算数量</td>
                        <td algin='center' style="width:100px;">实际配送</td>
                    </tr>
                </thead>
                <tbody id="tbodyHtml">
                </tbody>
            </table>
        </div>
	</div>

	<footer class="aui-bar aui-bar-tab" id="footer">
			<div id="yjzBtn" class="aui-btn aui-btn-info aui-btn-block" onclick="save()" style="display:none;">一键转库</div>
			<div id="yjcBtn" class="aui-btn aui-btn-info aui-btn-block" onclick="saveOUT()" style="display:none;">一键出库</div>
	</footer>

</body>
<script type="text/javascript" src="../script/api.js"></script>
<script type="text/javascript" src="../script/jquery-1.9.1.min.js"></script>
<script type="text/javascript" src="../script/aui-list-swipe.js"></script>
<script type="text/javascript" src="../script/public.js"></script>
<script type="text/javascript">
//光标所在id
var idSelector = "";
	apiready = function(){
		$("#paTaskCode").focus();
        idSelector = "paTaskCode";
	};

	var swipe = new ListSwipe();
  var paTaskCode ="";


	// 扫描配送任务号
	function doScanDaqInfo() {
		var moduleScanner = api.require('moduleScanner');
		var params = {
		top:150,//扫描窗口的上边距
		width:250,//扫描窗口的宽
		height:250,//扫描窗口的高
		useColor:false,//是否使用自己设定的颜色(startColor和endColor),
					   //如果不使用设定的颜色就使用默认的那张网格图
		startColor:'#0F08C8FF',//渐变起始颜色
		endColor:'#7F08C8FF',//渐变结束颜色
		animTime:2200,//扫描动画时间,单位为毫秒
		};
		moduleScanner.toScannerActivityForResult(params, function(ret, err){
			//查询配送任务单-对应的 物料需求信息
			getDistributeMrlInfo(ret);
		});
	}

    //扫描回车事件
    $("#paTaskCode").on('keypress', function(e) { //#keyword为input文本框
        var keycode = e.keyCode;
        var sm = $(this).val();
        if (keycode == '13') {
            $("#paTaskCode").val(sm);
            getDistributeMrlInfo(sm);
        }
    });


	// 获取工配送任务单-对应的 物料需求信息
	function getDistributeMrlInfo(code)
	{
		var serverUrl = localStorage.getItem('serverUrl');
		var url = serverUrl + "/app/searchDistributeMrlInfo";
		var params = new Object();
		params.url = url;
		params.method = 'post';
		params.dataType = 'json';
		var un = localStorage.getItem('un');
		var data = {
			values: {
				paTaskCode : code,
                un : un
	        }
		};
		console.log("un",un);
        api.showProgress({
            title: '保存中...',
        });
				api.ajax({
					url: url,
					method: 'post',
					dataType: 'json',
					timeout: 300,
					returnAll: false,
					data: data
				}, function(ret, err) {
            if (ret) {
                api.hideProgress();
								var data = JSON.stringify(ret);
								var myobj = eval("(" + data + ")");
                $("#paTaskCode").focus();
                idSelector = "paTaskCode";
                //返回成功
                if (myobj.code == "0") {
                    var list = myobj.list;
										var stockUpType=myobj.stockUpType;//（业务参数配置）配送任务备货处理类型， 一键出库、一键转库、不处理
                    var mainData=myobj.mainData;
                    var code=mainData.code;
                    var workOrderCode=mainData.workOrderCode;
                    var uda1c=mainData.uda1c;
                    var uda2c=mainData.uda2c;

										workOrderCode=workOrderCode==null?"":workOrderCode;
										uda2c=uda2c==null?"":uda2c;
                    $("#code").text(code);
                    $("#workOrderCode").text(workOrderCode);
                    $("#uda1c").text(uda1c);
                    $("#uda2c").text(uda2c);

                    var html = "";
										console.log("list.length:"+list.length);
                    //遍历
                    for (var i = 0; i < list.length; i++) {
                        var bgColor="#F5F5F5";
                        var uda1=list[i].ext01;
                        uda1=(uda1==null||uda1==undefined||uda1=="")?"0":uda1;
												console.log("countQty"+Number(list[i].countQty));
												console.log("uda1"+Number(uda1));
                        if (Number(list[i].countQty) > Number(uda1)) {
                            console.log("不满足");
                            bgColor="#f18e8e";
                        }else{
                            console.log("满足");
                            bgColor="#2ecc71";
                        }

                        html += "<tr style='text-align: center;color: black;font-size: 12px;background-color: "+bgColor+";' onclick='goDetailPge(\""+list[i].mrlCode+"\",\""+code+"\");'>";
                        html += "<td name='mrlCode'>" + list[i].mrlCode + "</td>";
                        html += "<td name='mrlName'>" + list[i].mrlName + "</td>";
                        html += "<td name='predictQty'>" + list[i].predictQty + "</td>";
                        html += "<td name='countQty'>" + list[i].countQty + "</td>";
                        html += "<td name='qty'>" + uda1 + "</td>";
                        html += "</tr>";
                    }
                    $api.html($api.byId("tbodyHtml"), html);

										//stockUpType 为不处理，则隐藏一键转库、一键出库按钮；
										//为一键出库，则显示一键出库按钮，隐藏一键转库按钮；
										//为一键转库，则显示一键转库按钮，隐藏一键出库按钮；
										if(stockUpType=="不处理"){
											document.getElementById("yjzBtn").style.display="none";
											document.getElementById("yjcBtn").style.display="none";
										}else if(stockUpType=="一键出库"){
											document.getElementById("yjzBtn").style.display="none";
											document.getElementById("yjcBtn").style.display="block";
										}else if(stockUpType=="一键转库"){
											document.getElementById("yjzBtn").style.display="block";
											document.getElementById("yjcBtn").style.display="none";
										}

                } else if (myobj.code == "1") {
                    //失败
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                }
            }
        });

	}
	
    //跳转任务明细页面
    function goDetailPge(mrlCode,paTaskCode){
        var url = "paAndonDistribute-detail.html";
		api.openWin({
            name: 'paAndonDistribute-detail',
            url: 'paAndonDistribute-detail.html',
            pageParam: {
                paTaskCode: paTaskCode,
                mrlCode: mrlCode
            }
        });
    }

		//一键转库按钮
		function save() {


		var obj = document.getElementById("code");
		var paTaskCode=obj.innerText;//$("#code").val();
		api.confirm({
				title: '提示',
				msg: "确认对配送任务单 【" + paTaskCode + "】进行一键转库操作？",
				buttons: ['确定', '取消']
		}, function (ret, err) {
				var index = ret.buttonIndex;
				if (index == 1) {
					api.showProgress({
							title: '保存中...',
					});
					var un = localStorage.getItem('un');
					var serverUrl = localStorage.getItem('serverUrl');
					var url = serverUrl + "/app/paDistributeBOX";
					var params = new Object();
					params.url = url;
					params.dataType = 'json';
					params.method = 'post';
					var data = {
							values: { ////以表单方式提交参数（JSON对象）
									paTaskCode: paTaskCode,
									un: un
							}
					};
					api.ajax({
						url: url,
						method: 'post',
						dataType: 'json',
						timeout: 300,
						returnAll: false,
						data: data
					}, function(ret, err) {
							if (ret) {

									var data = JSON.stringify(ret);
									var myobj = eval("(" + data + ")");
									//成功
									if (myobj.code == '0') {

											api.hideProgress();

											api.toast({
													msg: "保存成功",
													duration: 3000
											});
									} else  {
											api.hideProgress();
											api.toast({
													msg: myobj.msg,
													duration: 3000
											});
									}
							}
					});
				}
			}
		)


		}

		//一键转库按钮
		function saveOUT() {
		var obj = document.getElementById("code");
		var paTaskCode=obj.innerText;//$("#code").val();
		api.confirm({
				title: '提示',
				msg: "确认对配送任务单 【" + paTaskCode + "】进行一键出库操作？",
				buttons: ['确定', '取消']
		}, function (ret, err) {
				var index = ret.buttonIndex;
				if (index == 1) {
					api.showProgress({
							title: '保存中...',
					});
					var un = localStorage.getItem('un');
					var serverUrl = localStorage.getItem('serverUrl');
					var url = serverUrl + "/app/paDistributeOutBOX";
					var params = new Object();
					params.url = url;
					params.dataType = 'json';
					params.method = 'post';
					var data = {
							values: { ////以表单方式提交参数（JSON对象）
									paTaskCode: paTaskCode,
									un: un
							}
					};
					api.ajax({
						url: url,
						method: 'post',
						dataType: 'json',
						timeout: 300,
						returnAll: false,
						data: data
					}, function(ret, err) {
							if (ret) {

									var data = JSON.stringify(ret);
									var myobj = eval("(" + data + ")");
									//成功
									if (myobj.code == '0') {

											api.hideProgress();

											api.toast({
													msg: "保存成功",
													duration: 3000
											});
									} else  {
											api.hideProgress();
											api.toast({
													msg: myobj.msg,
													duration: 3000
											});
									}
							}
					});
				}
			}
		)
		}



</script>
</html>
